

// This do-file reproduces  Table A.2


version 14
use "data/reg_public_goods.dta" , clear


sort bgy_pop
gen ptile = int(100*(_n-1)/_N)+1

g herf_community_edge_t1 = herf_community_edge if ptile < 33.33
g herf_community_edge_t2 = herf_community_edge if ptile >  33.33 & ptile < 66.66
g herf_community_edge_t3 = herf_community_edge if ptile > 66.66


preserve

	g var_name ="."
	g out_mean = .
	g out_sd = .
	g out_median = .
	g out_min = .
	g out_max= .
	local i=1
	foreach x of varlist nb_communities share_top_community share_2nd_community herf_community_edge herf_community_walktrap ///
	herf_community_edge_t1 herf_community_edge_t2 herf_community_edge_t3  bgy_elem_school bgy_high_school bgy_market bgy_healthcentre bgy_waterworks {
		replace var_name = "`x'" in `i'
		su `x' , d
		replace out_mean = r(mean) in `i'
		replace out_sd = r(sd) in `i' 
		replace out_median = r(p50) in `i' 
		replace out_min = r(min) in `i'
		replace out_max = r(max) in `i'
		local i=`i'+1
		
	}
	
	format out_mean  out_sd out_min out_max  %9.3f
	
	foreach X in  out_sd   {
		capture drop test
		generate test = "(" + string(`X'  ,"%9.2f")  + ")" 
		drop `X'   
		rename test `X'
	
	}
	
	foreach X in   out_mean out_median out_min out_max {
		tostring `X'  , replace force format("%9.2f")
	
	}
	
	keep var_name   out_mean out_sd out_median  out_min out_max
	order var_name   out_mean out_sd out_median  out_min out_max
	keep if var_name!="."
	
	replace var_name = "Nb communities" if var_name=="nb_communities"
	replace var_name = "Share largest community" if var_name=="share_top_community"
	
	replace var_name = "Share 2nd largest community" if var_name =="share_2nd_community"
	replace var_name = "Fractionalisation (edge)" if var_name =="herf_community_edge"
	replace var_name = "Fractionalisation (edge) 1st tercile" if var_name =="herf_community_edge_t1"
	replace var_name = "Fractionalisation (edge) 2nd tercile" if var_name =="herf_community_edge_t2"
	replace var_name = "Fractionalisation (edge) 3rd tercile" if var_name =="herf_community_edge_t3"
	replace var_name = "Fractionalisation (walktrap)" if var_name =="herf_community_walktrap"
	replace var_name = "Elementary School" if var_name =="bgy_elem_school"
	replace var_name = "High School" if var_name =="bgy_high_school"
	replace var_name = "Market" if var_name =="bgy_market"
	replace var_name = "Health centre" if var_name =="bgy_healthcentre"
	replace var_name = "Waterworks" if var_name =="bgy_waterworks"
	
	logout  , save(results/table_a2_top.tex) clear tex replace dec(2)


	
	
use "data/reg_elections.dta" , clear


	g var_name ="."
	g out_mean = .
	g out_sd = .
	g out_median = .
	g out_min = .
	g out_max= .
	local i=1
	foreach x of varlist  nb_captain laakso golosov win_margin_bgy nb_councilors {
		replace var_name = "`x'" in `i'
		su `x' , d
		replace out_mean = r(mean) in `i'
		replace out_sd = r(sd) in `i' 
		replace out_median = r(p50) in `i' 
		replace out_min = r(min) in `i'
		replace out_max = r(max) in `i'
		local i=`i'+1
		
	}
	
	format out_mean  out_sd out_min out_max  %9.3f
	
	foreach X in  out_sd   {
		capture drop test
		generate test = "(" + string(`X'  ,"%9.2f")  + ")" 
		drop `X'   
		rename test `X'
	
	}
	
	foreach X in   out_mean out_median out_min out_max {
		tostring `X'  , replace force format("%9.2f")
	
	}
	
	keep var_name   out_mean out_sd out_median  out_min out_max
	order var_name   out_mean out_sd out_median  out_min out_max
	keep if var_name!="."
	
	replace var_name = "Number of candidates bgy cap" if var_name =="nb_captain"
	replace var_name = "Effective number of candidates bgy cap (Laakso)" if var_name =="laakso"
	replace var_name = "Effective umber of candidates bgy cap (Golosov)" if var_name =="golosov"
	replace var_name = "Win margin" if var_name =="win_margin_bgy"
	replace var_name = "Number of candidates bgy council" if var_name =="nb_councilors"
	
	logout  , save(results/table_a2_bottom.tex) clear tex replace dec(2)
